import 'package:flutter/material.dart';
import 'package:xiaotiancai/page/DetailPage.dart';
import 'package:xiaotiancai/style.dart';
class ShoppingCartPage extends StatefulWidget{
  const ShoppingCartPage({super.key});
  @override
  State<StatefulWidget> createState()=>_ShoppingCartPage();
}
class CarListBean{
  bool isCheck;
  String url;
  String title;
  int rest;
  int money;
  int num=1;
  CarListBean({
    required this.isCheck,
    required this.url,
    required this.title,
    required this.rest,
    required this.money,
    required this.num,
});
}
class _ShoppingCartPage extends State<ShoppingCartPage>{
  List<CarListBean>list=[
    CarListBean(url: "https://static-assets-prod.okii.com/xtc-products/z8/pc/scene-section/bg5.jpg", title:"小天才儿童手表E#", rest:999, money: 233, num:1, isCheck: false),
    CarListBean(url: "https://static-assets-prod.okii.com/xtc-products/z8/pc/scene-section/bg5.jpg", title:"小天才儿童手表E#", rest:999, money: 233, num:1, isCheck: false),
    CarListBean(url: "https://static-assets-prod.okii.com/xtc-products/z8/pc/scene-section/bg5.jpg", title:"小天才儿童手表E#", rest:999, money: 233, num:1, isCheck: false),
    CarListBean(url: "https://static-assets-prod.okii.com/xtc-products/z8/pc/scene-section/bg5.jpg", title:"小天才儿童手表E#", rest:999, money: 233, num:1, isCheck: false),
    CarListBean(url: "https://static-assets-prod.okii.com/xtc-products/z8/pc/scene-section/bg5.jpg", title:"小天才儿童手表E#", rest:999, money: 233, num:1, isCheck: false),


  ];

  bool checkAll=false;

  bool hasorder=false;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Container(
        color: Style.background,
        child: Column(children:[
          Container(
            color: const Color(0xffff7800),
            child: const Padding(
              padding: EdgeInsets.symmetric(
                  vertical: 20
              ),
              child: Center(
                child: Text("购物车",style: TextStyle(
                    fontSize: 16,color: Colors.white
                ),),
              ),
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemBuilder:(c,i)=>InkWell(
                onTap:(){
                  list[i].isCheck=!list[i].isCheck;
                  bool isCheckAll=true;
                  for (var element in list) {
                    if(!element.isCheck){
                      isCheckAll=false;
                    }
                  }
                  checkAll=isCheckAll;
                  setState(() {});
                },
                child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                  Row(children: [
                    Icon(list[i].isCheck?Icons.check_circle:Icons.check_circle_outline,color: Style.primaryColor,),
                    const SizedBox(width: 10,),
                    InkWell(
                      onTap:(){
                        Navigator.push(context, MaterialPageRoute(builder:(c)=>const DetailPage()));
                      },
                      child: Container(
                        width: 70,
                        height: 70,
                        decoration:BoxDecoration(
                            image: DecorationImage(
                                image:NetworkImage(list[i].url,),fit: BoxFit.cover),
                            borderRadius: const BorderRadius.all(Radius.circular(5))
                        ),),
                    ),
                    const SizedBox(width: 10,),
                    Column(crossAxisAlignment: CrossAxisAlignment.start,mainAxisAlignment: MainAxisAlignment.spaceAround,children:[
                      Text(list[i].title,style: const TextStyle(fontWeight: FontWeight.bold),),
                      Text("剩余：${list[i].rest}",style: const TextStyle(
                          color: Colors.grey
                      ),),
                      Text("¥${list[i].money}",style: TextStyle(
                          color: Style.primaryColor,
                          fontWeight: FontWeight.bold,
                          fontSize: 16
                      ),),
                    ],)
                  ],),
                  Row(children:[
                     InkWell(onTap:(){
                       list[i].num++;
                       setState(() {});
                     },child: const Padding(padding: EdgeInsets.all(5),child: Icon(Icons.add_circle_outline))),
                    Padding(padding: const EdgeInsets.symmetric(horizontal: 5),child: Text("${list[i].num}")),
                    InkWell(onTap:(){
                      if(list[i].num-1>0){
                        list[i].num--;
                        setState(() {});
                      }
                    },child: const Padding(padding: EdgeInsets.all(5),child: Icon(Icons.remove_circle_outline)))
                  ],)
                ],),
            ),
              ), itemCount: list.length,),
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(color: Colors.grey.withAlpha(100))
            ),
            child: IntrinsicHeight(
              child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
                InkWell(
                  onTap:(){
                    for (var element in list) {
                      element.isCheck=!checkAll;
                    }
                    checkAll=!checkAll;
                    setState(() {});
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(vertical: 15,horizontal: 20),
                    child: Row(children:[
                      checkAll?Icon(Icons.check_circle,color: Style.primaryColor,):const Icon(Icons.check_circle_outline),
                      const Text("全选")
                    ],),
                  ),
                ),
                Row(children:[
                  const Text("合计：",style: TextStyle(fontWeight: FontWeight.bold),),
                  Text(getAll().toString(),style: const TextStyle(
                      color: Colors.red,
                      fontWeight: FontWeight.bold
                  ),),
                ],),
                Container(
                  width: 100,
                  decoration: BoxDecoration(
                      color: hasorder?Style.primaryColor:Colors.grey.withAlpha(100)
                  ),child: Center(child: Text("结算",style: TextStyle(fontWeight: FontWeight.bold,
                color: hasorder?Colors.white:Colors.black),)),)
              ],),
            ),
          )

        ],),
      ),
    );
  }
  int getAll() {
    int money=0;
    for (var element in list) {
      if(element.isCheck){
        money+=element.money*element.num;
      }
    }
    hasorder=money>0;
    return money;
  }
}